<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Http\Requests\LiveCreatePost;
use App\library\services\ConfigService;
use App\library\services\LiveRecordingService;
use App\library\services\LiveService;
use Illuminate\Http\Request;

class LiveController extends Controller
{
    /**
     * Author:Zhongyuan Lu
     * 直播列表
     * @param Request $request
     * @return mixed
     */
    public function liveList(Request $request)
    {
        $page = $request->get('page', config('setting.api.page'));
        $limit = $request->get('limit', config('setting.api.limit'));
        $status = $request->get('status', 0);
        $ascription = $request->get('ascription', 0);
        $uid = $request->uid;
        if (isInt($page) || isInt($limit)) {
            return $this->fail('参数错误');
        }
        if (!in_array($status, config('setting.live.status'))) {
            $status = 0;
        }
        if (!in_array($ascription, config('setting.ascription'))) {
            $ascription = 0;
        }

        $data = LiveRecordingService::getLiveList($page, $limit, $status, $ascription, $uid);
        return $this->res($data);
    }

    /**
     * Author:Zhongyuan Lu
     * 经纪人发起直播
     * @param LiveCreatePost $request
     * @return mixed
     */
    public function createRecording(Request $request)
    {
        $uid = Request()->uid;
        $params = $request->input();

        $params['start_time'] = strtotime($params['start_date']." ".$params['start_time']);
        unset($params['start_date']);
        $params['end_time'] = strtotime($params['end_date']." ".$params['end_time']);
        unset($params['end_date']);

        $data = LiveRecordingService::createRecording($params,$uid);
        return $this->res($data);
    }

    /**
     * Author:Zhongyuan Lu
     * 经纪人直播详情
     * @param Request $request
     * @return mixed
     */
    public function edit(Request $request)
    {
        $id = $request->get('id',0);
        if (empty($id) && is_int($id)) {
            return $this->fail('参数错误');
        }

        $data = LiveRecordingService::getLiveEdit($id);

        if ($data['code'] == 400) {
            return $this->fail($data['message']);
        }
        return $this->success($data['message'],$data['data'],$data['code']);
    }

    /**
     * Author:Zhongyuan Lu
     * 直播回调
     */
    public function notifyUrl()
    {
        $data = file_get_contents('php://input');
        $params = json_decode($data, true);
        return LiveService::notifyUrl($params);
    }

    /**
     * Author:Zhongyuan Lu
     * 录播回调
     */
    public function recordNotify(){
        $data = file_get_contents('php://input');
        $params = json_decode($data, true);
        return LiveRecordingService::recordNotify($params);
    }

    /**
     * Author:Zhongyuan Lu
     * 前台直播页面
     * @param Request $request
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View
     */
    public function getPullUrl(Request $request)
    {
        $pull_url = $request->input('pull_url');
        return view('api/live',['pull_url'=>$pull_url]);
    }
}
